/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.notify.web;

import com.thinkgem.jeesite.androidpn.server.util.Config;
import com.thinkgem.jeesite.androidpn.server.xmpp.push.NotificationManager;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.notify.entity.NotiMessage;
import com.thinkgem.jeesite.modules.notify.service.NotiMessageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 推送消息Controller
 * @author 陈斌
 * @version 2018-12-13
 */
@Controller
@RequestMapping(value = "${adminPath}/notify/notiMessage")
public class NotiMessageController extends BaseController {

	private NotificationManager notificationManager;
	@Autowired
	private NotiMessageService notiMessageService;

	NotiMessageController() {
		notificationManager = new NotificationManager();
	}
	
	@ModelAttribute
	public NotiMessage get(@RequestParam(required=false) String id) {
		NotiMessage entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = notiMessageService.get(id);
		}
		if (entity == null){
			entity = new NotiMessage();
		}
		return entity;
	}
	
	@RequiresPermissions("notify:notiMessage:view")
	@RequestMapping(value = {"list", ""})
	public String list(NotiMessage notiMessage, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<NotiMessage> page = notiMessageService.findPage(new Page<NotiMessage>(request, response), notiMessage); 
		model.addAttribute("page", page);
		return "modules/notify/notiMessageList";
	}

	@RequiresPermissions("notify:notiMessage:view")
	@RequestMapping(value = "form")
	public String form(NotiMessage notiMessage, Model model) {
		model.addAttribute("notiMessage", notiMessage);
		return "modules/notify/notiMessageForm";
	}

	@RequiresPermissions("notify:notiMessage:edit")
	@RequestMapping(value = "save")
	public String save(NotiMessage notiMessage, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, notiMessage)){
			return form(notiMessage, model);
		}
        if ("--请选择--".equals(notiMessage.getPushAliasTag())) {
            notiMessage.setPushAliasTag(null);
        }

		if ("--请选择--".equals(notiMessage.getAppid())) {
			notiMessage.setAppid(null);
		}
		notiMessageService.save(notiMessage);

		if ("broadcast".equals(notiMessage.getPushType())) {
			notificationManager.sendBroadcast(Config.getString("apiKey", ""), notiMessage.getMsgTitle(), notiMessage.getMsgContent(), "http://xxx/ccc.html", notiMessage.getId(), notiMessage.getAppid(), notiMessage.getAppedition());
		}
		if ("alias".equals(notiMessage.getPushType()) || "tag".equals(notiMessage.getPushType())) {
			notificationManager.sendNotifcationToAliasOrTag(Config.getString("apiKey", ""), notiMessage.getPushAliasTag(), notiMessage.getPushType(), notiMessage.getMsgTitle(), notiMessage.getMsgContent(), "http://xxx/ccc.html", notiMessage.getId());
		}
		addMessage(redirectAttributes, "保存推送消息成功");
		return "redirect:"+Global.getAdminPath()+"/notify/notiMessage/?repage";
	}
	
	@RequiresPermissions("notify:notiMessage:edit")
	@RequestMapping(value = "delete")
	public String delete(NotiMessage notiMessage, RedirectAttributes redirectAttributes) {
		notiMessageService.delete(notiMessage);
		addMessage(redirectAttributes, "删除推送消息成功");
		return "redirect:"+Global.getAdminPath()+"/notify/notiMessage/?repage";
	}

	@RequestMapping("/pushMessageByTagOrAlias")
	@ResponseBody
	public List<String> getPushMessageTagOrAlias(String dictType) {
		if (StringUtils.isEmpty(dictType)) {
			return new ArrayList<String>();
		}
		List<String> pushMessageTagOrAlias = notiMessageService.getPushMessageTagOrAlias(dictType);
		if (pushMessageTagOrAlias.size() <= 0) {
			return new ArrayList<String>();
		}
		return notiMessageService.getPushMessageTagOrAlias(dictType);
	}
}